﻿using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;

namespace BasicModuleWebApiAuthorization.Authorization
{
    /// <summary>
    /// 自定义密码鉴权
    /// </summary>
    public class UserAuthorizationHandler : AuthorizationHandler<UserNameAuthorization> 
    {
        /// <summary>
        /// 开始鉴权
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requirement"></param>
        /// <returns></returns>
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserNameAuthorization requirement)
        {
            //context.Succeed(requirement);
            var conext = context.User.Claims.Where(x => x.Type == ClaimTypes.Name).FirstOrDefault();

            //获取接口权限
            var data = requirement.names.Where(x => x == conext.Value);
            //鉴权
            if (data != null && data.Count() > 0)
                context.Succeed(requirement);
            else
                context.Fail();
            return Task.CompletedTask;
        }
    }
}
